﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using Sherwood.Content.DO.Model;
using Xtensive.Storage;

namespace Sherwood.Content.DO.Services
{
    public class ClientRepository : IClientRepository
    {
        /// <inheritdoc/>
        public IEnumerable<IClient> All
        {
            get { return Query.All<Client>(); }
        }

        /// <inheritdoc/>
        public IClient this[string id]
        {
            get
            {
                var client = FindById(id).FirstOrDefault();
                if (client == null)
                    throw new ArgumentException(string.Format("Client {0} does not exist", id), "id");
                return client;
            }
        }

        /// <inheritdoc/>
        public bool TryGetById(string id, out IClient client)
        {
            client = FindById(id).FirstOrDefault();
            return client != null;
        }

        private static IEnumerable<IClient> FindById(string id)
        {
            return Query.All<Client>().Where(c => c.Id == id);
        }
    }
}
